package com.honey.solr.daosupport.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.generator.java.AbstractClassName;
import com.honey.solr.SolrConstant;
import com.honey.solr.SolrPlugin;

public class SetSolrServerUrlMethod extends AbstractClassName {
	
	private SolrPlugin solrPlugin = null;
	
	private AbstractJavaCompilation clazz = null;
	
	public SetSolrServerUrlMethod(SolrPlugin solrPlugin ,AbstractJavaCompilation clazz  ){
		this.solrPlugin = solrPlugin;
		this.clazz = clazz;
	}
	
	public Method createMethod(){
		Method answer = new Method("setSolrServerUrl");
		Parameter parameter = new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),"solrServerUrl");
		answer.addParameter(parameter) ;
		answer.addBodyLine("this.solrServerUrl = solrServerUrl");
		if( solrPlugin.getHttpClientVersion() <= 3){
			answer.addBodyLine("this."+SolrConstant.SOLR_SERVER_FIELD_NAME+" =  new CommonsHttpSolrServer( solrServerUrl )");
			clazz.addImportedType(new FullyQualifiedJavaType("org.apache.solr.client.solrj.impl.CommonsHttpSolrServer"));
		}else{
			answer.addBodyLine("this."+SolrConstant.SOLR_SERVER_FIELD_NAME+" =  new HttpSolrServer( solrServerUrl )");
			clazz.addImportedType(new FullyQualifiedJavaType("org.apache.solr.client.solrj.impl.HttpSolrServer"));
		}
		
		return answer ;
	}

}
